package com.kedi.usercenter.pclogin.controller;

import com.kedi.usercenter.applogin.controller.LoginController;
import com.kedi.usercenter.pclogin.service.AdminLoginService;
import com.kedi.usercenter.utils.LoginCommonUtil;
import com.kedi.usercenter.utils.ResultVoUtil;
import com.kedi.usercenter.vo.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * PC端管理员登录控制器
 * @author Zyred
 */
@RestController
@RequestMapping("/admin")
public class AdminLoginController {

    Logger logger = LoggerFactory.getLogger(AdminLoginController.class);

    @Autowired
    private AdminLoginService service;

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * PC端管理员用户登录
     * @param password 密码
     * @param loginName 登录名称
     * @return
     */
    @RequestMapping("/login")
    public ResultVo adminLogin(@RequestParam("password") String password,
                               @RequestParam("loginName") String loginName) throws Exception {

        if(password == null && password.isEmpty()){
            return ResultVoUtil.error(0, "密码不能为空哦-_-");
        }
        if(loginName == null && loginName.isEmpty()){
            return ResultVoUtil.error(0,"登录名不能为空哦-_-");
        }

        //对密码进行加密，后进行查询数据库
        String mPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        Map<String, Object> userMap = service.getAdminLoginInfo(loginName, mPassword);

        if(userMap==null){
            logger.info("用户不存在或账户密码错误");
            return ResultVoUtil.error(0,"用户不存在或账户密码错误");
        }else {
            //TODO
            List<String> urls = null;
            return LoginCommonUtil.commonLogin(userMap, urls, redisTemplate);
        }

    }

}
